package algorithm.middle;

import sun.misc.ObjectInputFilter;

import java.time.OffsetDateTime;
import java.util.Arrays;

public class MinimumSubarrayLength3095 {
    /**
     * 滑动窗口
     * 1.移动右指针，找到符合条件的数组
     */
    public int minimumSubarrayLength(int[] nums, int k) {
        //使用binary记录二进制位每个1出现的个数
        int[] binary = new int[30];
        int left = 0, right = 0;
        int res = Integer.MAX_VALUE;
        while (right < nums.length) {
            for (int i = 0; i < 30; i++) {
                binary[i] +=  (nums[right] >> i) & 1;
            }
            //符合k条件则缩小窗口，移动左指针
            while (left <= right && calculate(binary) >= k) {
                res = Math.min(res, right - left + 1);
                //调整binary
                for (int i = 0; i < 30; i++) {
                    binary[i] -= (nums[left] >> i) & 1;
                }
                left++;
            }
            //增加右指针
            right++;
        }
        return res==Integer.MAX_VALUE?-1:res;
    }

    public int calculate(int[] binary) {
        int res = 0;
        for (int i = 0; i < 30; i++) {
            if (binary[i] > 0) {
                //此二进制位是1
                res |= 1 << i;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        new MinimumSubarrayLength3095().convert("AB", 1);
    }


    public  String convert(String s, int numRows) {
        if (numRows == 1) {
            return s;
        }
        int rowIndex = 0;
        boolean add = true;
        String[] arr = new String[numRows];
        Arrays.fill(arr, "");
        for (int i = 0; i < s.length(); i++) {
            arr[rowIndex] += s.charAt(i);
            rowIndex = add ? rowIndex + 1 : rowIndex - 1;
            if (rowIndex == numRows-1) {
                add = false;
            } else if (rowIndex == 0) {
                add = true;
            }
        }
        String res = "";
        for (String string : arr) {
            res += string;
        }
        return res;
    }
}
